﻿/*
 * Author: Roy Scheefhals
 * 
 * XML writer that writes licence plate information to an xml file
 * */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Xml;
using System.IO;
using System.Drawing;

namespace LibraryCS
{
    /// <summary>
    /// Write XML file with the format of the input xml from Daniël Telgen
    /// </summary>
    class XMLIO
    {
        DataSet ds;
        XmlWriter writer;
        /// <summary>
        /// standard constructor
        /// XMLIO needs to be closed using Close()
        /// </summary>
        public XMLIO()
        {
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;
            settings.IndentChars = "\t";

            ds = new DataSet();
            writer = XmlWriter.Create(Directory.GetCurrentDirectory() + "\\XMLoutput.xml",settings);
            writer.WriteStartElement("testsamples");
        }

        /// <summary>
        /// add new image element the license plate will be 00-ABC-0
        /// </summary>
        /// <param name="upperLeft">top left of the license plate</param>
        /// <param name="upperRight">top right of the license plate</param>
        /// <param name="lowerLeft">bottom left of the license plate</param>
        /// <param name="lowerRight">bottom right of the license plate</param>
        /// <param name="imagefilename">filename of the image location ( needed for compability )</param>
        public void addDataSet(Point upperLeft, Point upperRight, Point lowerLeft, Point lowerRight, String imagefilename)
        {

            writer.WriteStartElement("image");
                writer.WriteAttributeString("filename", imagefilename);

                writer.WriteStartElement("license_plate");
                    writer.WriteStartElement("location");


                         //upper left attribute of the location
                        writer.WriteStartElement("upperleft");
                            writer.WriteAttributeString("x", upperLeft.X.ToString());
                            writer.WriteAttributeString("y", upperLeft.Y.ToString());
                        writer.WriteEndElement();// end upperleft

                         //upper right attribute of the location
                        writer.WriteStartElement("upperright");
                            writer.WriteAttributeString("x", upperRight.X.ToString());
                            writer.WriteAttributeString("y", upperRight.Y.ToString());
                        writer.WriteEndElement();
                        
                        //lower left attribute of location
                        writer.WriteStartElement("lowerleft");
                            writer.WriteAttributeString("x", lowerLeft.X.ToString());
                            writer.WriteAttributeString("y", lowerLeft.Y.ToString());
                        writer.WriteEndElement();
                        
                        //lower right attribute of location
                        writer.WriteStartElement("lowerright");
                            writer.WriteAttributeString("x", lowerRight.X.ToString());
                            writer.WriteAttributeString("y", lowerRight.Y.ToString());
                        writer.WriteEndElement();
                        
                    writer.WriteEndElement();// end of location

                    writer.WriteElementString("text", "00-ABC-0");

                writer.WriteEndElement();//end of license plate

            writer.WriteEndElement();// end the image

        }

        /// <summary>
        /// ends remaining tags and closes the XML file
        /// </summary>
        public void CloseXML()
        {
            writer.WriteEndElement();
            writer.Close();
        }
    }
}
